package com.itboye.mengwu.app.ui.mymeng;

import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.itboye.core.alert.ByAlert;
import com.itboye.core.object.BaseActivity;
import com.itboye.core.object.ResultEntity;
import com.itboye.core.object.SpPublicParams;
import com.itboye.core.utils.SPUtils;
import com.itboye.mengwu.R;
import com.itboye.mengwu.app.ui.aizhiwu.GeRenActivity;
import com.itboye.mengwu.app.ui.aizhiwu.GeRenKongJianActivity;
import com.itboye.mengwu.data.model.FenSiBean;
import com.itboye.mengwu.data.model.FenSiBean.FenSiModel;
import com.itboye.mengwu.data.model.RedBagModel;
import com.itboye.mengwu.data.presenter.FenSiPresenter;
import com.itboye.mengwu.data.presenter.RedBagPresenter;
import com.itboye.mengwu.widget.XListView;
import com.itboye.mengwu.widget.XListView.IXListViewListener;
import com.itboye.shopcar_adapter.FollowAdapter;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class FollowActivity extends BaseActivity implements Observer,
		IXListViewListener {
	// 自定义listview
	private XListView listviewFollow;
	//
	private String uid;
	private String touid;
	private FollowAdapter adapter;
	private FenSiPresenter bagPresenter;
	private TextView txt_FenSititle;
	int page_no = 1;
	// 实体类
	FenSiBean model;
	int posion;

	@Override
	public int initView() {

		return R.layout.activity_follow;
	}

	public void onMyonlickBack(View view) {
		this.finish();
	}

	@Override
	public void onMyCreate() {

		txt_FenSititle.setText("我的粉丝");
		txt_FenSititle.setTextSize(20);
		// 刷新
		listviewFollow.setXListViewListener(this);
		listviewFollow.setPullLoadEnable(true);
		listviewFollow.setPullRefreshEnable(true);
		listviewFollow.setAdapter(adapter);
		showProgressDialog("数据加载中，请稍后...", true);
		bagPresenter = new FenSiPresenter(this);
		model = new FenSiBean();
		uid = SPUtils.get(FollowActivity.this, null, SpPublicParams.UID, "")
				+ "";
		System.out.println("粉丝Uid" + uid);
		model.setUid(uid);

		model.setPage_no(page_no + "");
		bagPresenter.getInfo(model);

	}

	@SuppressWarnings("unchecked")
	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {

			if (entity.getEventType() == bagPresenter.FenSiSuccess) {
				model = (FenSiBean) entity.getData();
				adapter = new FollowAdapter(model, FollowActivity.this);
				listviewFollow.setAdapter(adapter);

				listviewFollow
						.setOnItemClickListener(new OnItemClickListener() {

							@Override
							public void onItemClick(AdapterView<?> arg0,
									View arg1, int arg2, long arg3) {
								// TODO Auto-generated method stub

							}
						});
			}

			try {
				/**
				 * 当数据加载出来后dialog则消失
				 */
				closeProgressDialog();
			} catch (Exception e) {
				ByAlert.alert("暂无数据！");
			}

			stopLoad();
		} else {
			ByAlert.alert("数据解析错误！");
		}
	}

	public void onFenSiBack(View view) {
		this.finish();
	}

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		page_no = 1;
		model.setUid(uid);
		bagPresenter.getInfo(model);

	}

	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		page_no++;
		model.setUid(uid);
		bagPresenter.getInfo(model);
	}

	public void stopLoad() {
		listviewFollow.stopLoadMore();
		listviewFollow.stopRefresh();
	}

}
